#include <WiFi.h>
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI();  // Invoke library, pins defined in User_Setup.h


#define TFT_GREY 0x5AEB // New colour

uint16_t screenWidth=0;
uint16_t screenHeight=0;

uint16_t centerX = 0;
uint16_t centerY = 0;

int fontSize = 4;        // 字号大小

const char* ssid     = "CAN_WIFI"; // Change this to your WiFi SSID
const char* password = "20240326"; // Change this to your WiFi password
#define TCPSER "192.168.4.1"
#define TCPPORT 23

int field1 = 0;
#define MESH_ID 5555555
typedef struct esp_frame_t {
  int mesh_id;
  unsigned long can_id;
  uint8_t len;
  uint8_t d[8];
};
esp_frame_t esp_frame;

void setup()
{
  tft.init();
  tft.setRotation(0);
   // 获取屏幕尺寸
  screenWidth = tft.width();
  screenHeight = tft.height();

  // 计算屏幕中心坐标
  centerX = screenWidth / 2;
  centerY = screenHeight / 2;
  tft.fillScreen(TFT_GREY);
  //tft.setCursor(x, y);
  tft.drawCentreString("Connecting to",120,110,1);
  tft.drawCentreString(ssid,120,130,1 );
 
    
    Serial.begin(115200);
    while(!Serial){delay(100);}

    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println("******************************************************");
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);
    
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
        //tft.drawCentreString(".",120,150,1);
    }
    

  //背景颜色
   tft.fillScreen(TFT_GREY);

  tft.setTextColor(TFT_WHITE);
  tft.setTextSize(1);

  String text = "WiFi connected";
  int x = screenWidth  / 2;
  int y = screenHeight / 2;
  tft.setCursor(x, y);
  tft.drawCentreString("WiFi connected",120,110,1);
  tft.drawCentreString("IP address:",120,130,1 );
  tft.drawCentreString(WiFi.localIP().toString(),120,150,1);


  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

WiFiClient client;
byte i=0;

void loop(){

  if (!client.connected())
  {
      Serial.println("tcp not connect");
      client.connect(TCPSER, TCPPORT); 
  }
  else
  {
  while(client.available()){
          //get data from the telnet client and push it to the UART
            Serial.print("data available:");
            Serial.println(client.available());
            client.read((uint8_t *) &esp_frame,sizeof(esp_frame_t));
            if(esp_frame.can_id == 0x257)
            {    
                String text;
                shift_data(esp_frame.d,&text);
                //lcd
                tft.fillScreen(TFT_GREY);
                
                //String text = String(0);
                tft.setTextSize(2);
                tft.drawCentreString(text,120, 70,7);
                
            }
          
        }
    
  
  }
  //delay(1000);
}

/*
//can_id  0x257
| display name | start bit| length | byte order | value type |
|di speed checksum|   0   |   8    |   little   | integer    |
|di speed counter | 8     |   4    |   little   | integer    |
| vehicle Speed|    12    |   12   |   little   | double     |
| UI speed     |  24      |  9     |   little   |   integer  |
| Speed units  |   33     |  1     |   1:mph 0: kph          |
|uispeedhighspeed| 34     |   9    | little     |   integer  |
*/

void shift_data(const uint8_t *candata,String *str_speed)
{
 uint8_t DI_speedChecksum;
 uint8_t DI_speedCounter;
 uint16_t DI_vehicleSpeed;
 uint16_t UISpeed;

 DI_speedChecksum=candata[0];
  //Serial.print("DI_speedChecksum(hex):");
  //Serial.println(DI_speedChecksum,HEX);

   

 DI_speedCounter=candata[1] & 0xf;
 //Serial.print("DI_speedCounter(hex):");
 //Serial.println(DI_speedCounter,HEX);

 DI_vehicleSpeed = (candata[1] & 0xf0)>>4;
 DI_vehicleSpeed+=candata[2]<<4;
 //Serial.print("DI_vehicleSpeed(hex):");
 //Serial.println(DI_vehicleSpeed,HEX);

 UISpeed = (candata[4]& 0x1)<<8;
 UISpeed = UISpeed+candata[3];
 //Serial.print("UISpeed(hex):");
 //Serial.println(UISpeed);
 *str_speed=String(UISpeed);
}

struct ID257Dispeed{
  uint8_t DI_speedChecksum;

  uint16_t DI_speedCounter:4;
  uint16_t DI_vehicleSpeed:12;

  uint32_t UISpeed:9;
  uint32_t DI_uiSpeedUnits:1;
  uint32_t DI_uispeedhighSpeed:12;
  uint32_t :10; //占位

  uint8_t  :8; //占位

}ID257D;



void can2speed(const uint8_t *candata,int *uispeed,double *vspeed)
{
  double aa;
  int bb;
  memset(&ID257D,0, sizeof(struct ID257Dispeed));
  memcpy(&ID257D,candata,sizeof(struct ID257Dispeed));
  //uispeed=ID257D.UISpeed;
  aa=0x0;
  bb=0x0;
  bb+=ID257D.UISpeed; //9bits & 0x1ff
  aa+=ID257D.DI_vehicleSpeed; //  12 bits & 0xFFF
  //vspeed=(double) ID257D.DI_vehicleSpeed;

   Serial.print("DI_speedChecksum(hex):");
   Serial.println(ID257D.DI_speedChecksum,HEX);

   Serial.print("DI_speedCounter(hex):");
   Serial.println(ID257D.DI_speedCounter,HEX);

  Serial.print("vehicleSpeed:");
  Serial.print(aa);
  Serial.print(" hex:");
   Serial.print(ID257D.DI_vehicleSpeed,HEX);
  Serial.print(" UISpeed:");
  Serial.print(bb);
  Serial.print(" hex:");
  Serial.println(ID257D.UISpeed,HEX);
}
/*
51km /h
40 37 44 2F 02    //18 22 01 00 00 00
40  checksum
37 44  counter & vehicle Speed
小端： 44 37
0100 counter 
0100 0011 0111  vehicle Speed 1079


2F 02 UI speed  &  Speed units  
小端： 02 2f 
 1  Speed units  
 0 0010 1111 UI speed  47km/h
*/